#ifndef  MY_MEM_POOL_H 
#define  MY_MEM_POOL_H 1

#include <config.h>
#include <core.h>


typedef struct pool_data_t {
   char             *end;
   char             *last;
   struct pool_t    *next;
   size_t           failed; 
} pool_data_t;


typedef struct pool_large_t {
  void                  *alloc;
  struct pool_large_t   *next; 
} pool_large_t;

typedef void (*pool_cleanup_handle_pt)(void *);

typedef struct pool_cleanup_t {
  pool_cleanup_handle_pt    handler;
  void                      *data;
  struct pool_cleanup_t     *next; 
} pool_cleanup_t;

#define PALLOC_SMALLEST_SIZE sizeof(pool_recycle_t)
typedef struct pool_recycle_t {
  //void                *pos;
  size_t                len;
  struct pool_recycle_t *next; 
} pool_recycle_t;

typedef struct pool_t {
   pool_data_t      d;
   size_t           max;
   struct pool_t    *current;
   pool_large_t     *large; 
   pool_cleanup_t   *cleanup; 
   pool_recycle_t   *recycle; 
} pool_t;

#ifdef  __cplusplus
extern "C" {
#endif

pool_t * 	create_pool(size_t size);
void  		destroy_pool(pool_t *pool);
void 		reset_pool(pool_t *pool);
void   *        pool_palloc(pool_t *pool,size_t size);
void   *        pool_calloc(pool_t *pool,size_t size);
void   *        pool_realloc(pool_t *pool,void *data,size_t osize,size_t size);

void   * 	pool_palloc_from_recycle(pool_t *pool,size_t size);
void   * 	pool_palloc_block(pool_t *pool,size_t size);
void   * 	pool_palloc_large(pool_t *pool,size_t size);

unsigned int    pool_free(pool_t *pool,void *pdata,size_t len);
unsigned int 	pool_recycle(pool_t *pool,void *pdata,size_t len);
unsigned int    pool_pfree(pool_t *pool,void *p);

pool_cleanup_t * pool_cleanup_add(pool_t *pool,size_t size);

#ifdef  __cplusplus
}
#endif

#endif //MY_MEM_POOL_H
